package com.jsyh.maidu.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.ArticleListActivity;
import com.jsyh.maidu.model.Article;

import java.util.List;

/**
 * Created by ZHY_9 on 2017/7/12.
 */

public class ArticleExpandableAdapter extends BaseExpandableListAdapter {


    List<Article> datas;
    Context context;

    public ArticleExpandableAdapter(Context context, List<Article> datas) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getChildren().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return datas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).getChildren().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.expand_parent_item, null);
        TextView tv = (TextView) view.findViewById(R.id.article_parent_title);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ArticleListActivity.class);
                intent.putExtra("cat_id", datas.get(groupPosition).getCat_id());
                context.startActivity(intent);
            }
        });
        ImageView expand = (ImageView) view.findViewById(R.id.article_parent_expand);
        tv.setText(datas.get(groupPosition).getCat_name());
        if (datas.get(groupPosition).getChildren().size() > 0){
            expand.setVisibility(View.VISIBLE);
        }else expand.setVisibility(View.GONE);
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.expand_child_item, null);
        TextView tv = (TextView) view.findViewById(R.id.expand_child_title);
        String title = datas.get(groupPosition).getChildren().get(childPosition).getCat_name();
        if (!TextUtils.isEmpty(title)){
            tv.setText(title);
        }
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
